﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.SessionState;

namespace skvote
{
    public static class function
    {
        public static void header(HttpResponse response)
        {
            response.Write("<meta http-equiv=\"Content-Language\" content=\"zh-cn\">");
            response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html\" charset=\"gb2312\">");
            response.Write("<meta http-equiv=\"author\" author=\"单考\">");
            response.Write("<meta http-equiv=\"keyword\" content=\"单考,单考投票,单考软件,程序开发,程序定制\">");
            response.Write("<meta http-equiv=\"description\" content=\"www.xhway.net,www.xhway.com,www.xhway.cn,单考软件拥有开发从C,C++,.NET,JAVA,ASP,PHP,DELPHIE等大部分语言WEB程序与软件系统的技术实力，欢迎大家定制各类程序！\">");
            response.Write("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\"/>");            
        }
        public static void showErr(string msg,HttpResponse response,HttpRequest request)
        {
            response.Write("<html>");
            response.Write("<head>");
            header(response);
            response.Write("<title>【单考投票系统】</title>");
            response.Write("</head>");
            response.Write("<body>");
            response.Write("<table width=\"40%\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\" bgcolor=\"#BBDDFF\" id=\"table2\" style=\"border-collapse: collapse; margin-top:50px;\">");
            response.Write("<tr>");
            response.Write("<td bgcolor=\"#ECF5FF\" align=\"center\" height=\"100\">");
            response.Write("<label id=\"errmsg\" name=\"errmsg\">" +msg+ "</label><br/><br/>");
            response.Write("<a href=\"" +request.ServerVariables["HTTP_REFERER"]+ "\">点击返回</a>");
            response.Write("</td>");
            response.Write("</tr>");
            response.Write("</table>");
            response.Write("</body>");
            response.Write("</html>");
            response.End();
        }
        public static string login()
        {
            System.Text.StringBuilder htmls = new System.Text.StringBuilder();
            htmls.Append("}");
            htmls.Append("</script>");
            htmls.Append("<table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"table1\" style=\"border-collapse: collapse\">");
            htmls.Append("<tr>");
            htmls.Append("<td valign=\"top\">");
			htmls.Append("<table width=\"40%\" border=\"1\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\" bgcolor=\"#BFDFFF\" id=\"table2\" style=\"border-collapse: collapse; margin-top:50px;\">");
            htmls.Append("<form action=?act=login method=post name=form1 onSubmit=\"return check()\">");
            htmls.Append("<tr id=\"TableTitle2\">");
     		htmls.Append("<th colspan=\"2\">【单考投票系统】后台管理登录</th>");
			htmls.Append("</tr>");
			htmls.Append("<tr>");
			htmls.Append("<td width=\"40%\" align=\"right\" bgcolor=\"#ECF5FF\"><b>用户名：</b></td>");
			htmls.Append("<td bgcolor=\"#ECF5FF\"><input type=\"text\" name=\"username\"><label id=\"lbluser\"></label></td>");
			htmls.Append("</tr>");
			htmls.Append("<tr>");
			htmls.Append("<td width=\"40%\" align=\"right\" bgcolor=\"#ECF5FF\"><b>密&nbsp; 码：</b></td>");
			htmls.Append("<td bgcolor=\"#ECF5FF\"><input type=\"password\" name=\"password\"><label id=\"lblpass\"></label></td>");
	    	htmls.Append("</tr>");
			htmls.Append("<tr>");
			htmls.Append("<td align=\"right\" bgcolor=\"#ECF5FF\"><b>验证码：</b></td>");
			htmls.Append("<td bgcolor=\"#ECF5FF\"><input type=\"text\" name=\"yz\" size=\"8\"> <img src=\"GetCode.asp\" onClick=\"this.src='GetCode.asp'\">&nbsp;<label id=\"lblyz\"></label></td>");
			htmls.Append("</tr>");
			htmls.Append("<tr>");
		    htmls.Append("<td height=\"30\" colspan=\"2\" align=\"center\" bgcolor=\"#ECF5FF\"><input class=\"btn\" type=\"submit\" name=\"submit\" value=\"登 陆\"> <input class=\"btn\" type=\"reset\" name=\"reset\" value=\"取 消\"></td>");
			htmls.Append("</tr>");
			htmls.Append("</form>");
		    htmls.Append("</table>");
		    htmls.Append("</td>");
		    htmls.Append("</tr>");
            htmls.Append("</table>");
            return htmls.ToString();
        }
        public static string loginout(HttpSessionState session)
        {
            session["admin"] = "";
            return login();
        }
        public static string nohtml(string input)
        {
            //删除脚本
            input = System.Text.RegularExpressions.Regex.Replace(input, @"<script[^>]*?>.*?</script>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //删除html
            input = System.Text.RegularExpressions.Regex.Replace(input, @"<(.[^>]*)>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"([\r\n])[\s]+", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"-->", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"<--.*", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"&(quot|#34);", "\"", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"&(amp|#38);", "&", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"&(lt|#60);", "<", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"&(gt|#62);", ">", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"&(nbsp|#160);", " ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"&(iexcl|#161);", "\xa1", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"&(cent|#162);", "\xa2", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"&(pound|#163);", "\xa3", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"&(copy|#169);", "\xa9", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            input = System.Text.RegularExpressions.Regex.Replace(input, @"&#(\d+);", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            input = input.Replace("<", "");
            input = input.Replace(">", "");
            input = input.Replace("\r\n", "");
            return HttpContext.Current.Server.HtmlEncode(input).Trim();
        }
        public static void check()
        {
            if (HttpContext.Current.Session["admin"] == null || HttpContext.Current.Session["admin"].ToString() == string.Empty)
            {
                HttpContext.Current.Response.Write("<script language='javascript' type='text/javascript'>alert('登录失败或超时,请重新登录');location.href='login.aspx';</script>");
                HttpContext.Current.Response.End();
            }            
        }
        public static void successMsg(string message)
        {
            HttpResponse response = HttpContext.Current.Response;
            response.Write("<html>");
            response.Write("<head>");
            header(response);
            response.Write("<title>【单考投票系统】</title>");
            response.Write("</head>");
            response.Write("<body>");
            response.Write("<table width=\"40%\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\" bgcolor=\"#BBDDFF\" id=\"table2\" style=\"border-collapse: collapse; margin-top:50px;\">");
            response.Write("<tr>");
            response.Write("<td bgcolor=\"#ECF5FF\" align=\"center\" height=\"100\">");
            response.Write("<label id=\"msg\" name=\"msg\">" + message + "</label><br/><br/>");
            response.Write("<a href=\"" + HttpContext.Current.Request.ServerVariables["HTTP_REFERER"] + "\">点此返回操作</a>&nbsp&nbsp");
            response.Write("<a href=\"admin_index.aspx\" target='_top'>进入管理首页</a>");
            response.Write("</td>");
            response.Write("</tr>");   
            response.Write("</table>");
            response.Write("</body>");
            response.Write("</html>");
            response.End();    
        }
        public static void IsNumberOrEmpty(string sid)
        {
            HttpResponse Response = HttpContext.Current.Response;
            if (sid == null || sid == "" || !IsNumber(sid))
            {
                showErr("参数不能为空且只能是数字", Response, HttpContext.Current.Request);
            }            
        }
        private static bool IsNumber(string s)
        {
            bool IsNum = true;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] >= '0' && s[i] <= '9')
                {
                    IsNum = true;
                }
                else
                {
                    IsNum = false;
                    break;
                }
            }
            return IsNum;
        }

    }
}
